<template>
  <el-dialog :title="title" :visible.sync="visible" width="1000px">
    <div class="add-form">
      <el-form ref="form" :model="form" label-width="120px">
        <el-form-item :rules="[{ required: true, message: '请选择渠道', trigger: 'change' }]" label="渠道" prop="channelKey">
          <XmSelect filterable clearable v-model="form.channelKey" placeholder="请选择渠道" :options="channelList">
          </XmSelect>
        </el-form-item>
        <el-form-item label="词汇作用" prop="tag" :rules="[{ required: true, message: '请选择词汇作用', trigger: 'change' }]">
          <XmSelect filterable clearable v-model="form.tag" placeholder="词汇作用" :options="wordEffectList">
          </XmSelect>
        </el-form-item>
        <el-form-item label="词库内容" :rules="[{ required: true, message: '请输入词库内容，例如：词汇1、词汇2', trigger: 'blur' }]"
          prop="content">
          <el-input type="textarea" v-model="form.content" placeholder="请输入词库内容，例如：词汇1、词汇2"></el-input>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="form.remark" placeholder="请输入备注"></el-input>
        </el-form-item>

        <el-form-item label="状态">
          <el-switch v-model="form.state"></el-switch>
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { saveWord, updateWord } from '@/api/maintainData/word'
import { wordEffectList } from '@/const'
export default {
  props: {
    channelList: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      visible: false,
      type: 'add',
      form: {
        channelKey: '',
        tag: '',
        content: '',
        remark: '',
        state: true,
      },
      wordEffectList
    }
  },
  methods: {
    show({ type, data }) {
      this.type = type
      this.visible = true
      if (data) {
        this.form = {
          ...data,
          state: data.state ? true : false,
          channelKey: data.channelKey === 0 ? '0' : data.channelKey,
        }
      } else {
        this.clearForm()
      }
    },
    clearForm() {
      this.form = {
        channelKey: '',
        tag: '',
        content: '',
        remark: '',
        state: true,
      }
    },
    submit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          const request = this.type === 'add' ? saveWord : updateWord
          request(this.getParams()).then(res => {
            if (res.data.code === 200) {
              this.$message({
                type: 'success',
                message: `${this.title}成功！`
              })
              this.visible = false
              this.$emit('submit')
            } else {
              this.$message({
                type: 'error',
                message: res.data.msg
              })
            }
          })
        }
      })
    },
    getParams() {
      const params = {
        ...this.form,
        state: this.form.state ? 1 : 0,
      }
      return params
    }
  },
  computed: {
    title() {
      const textMap = {
        'add': '新增',
        'edit': '编辑',
      }
      return textMap[this.type]
    }
  },
}
</script>

<style lang="scss" scoped>
</style>
